package com.sharp.mq.config;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;

import java.io.IOException;

/**
 * Title: BaseMessageDelegate
 * Description: Copyright: Copyright (c) 2019 Company: BHFAE
 *
 * @author Sharp
 * @date 2021/7/4 19:51
 */
public abstract class MessageDelegate<T> {

    public void handleMessage(T messageBody, Message message, Channel channel) throws IOException {
        try {
            handleMessage(messageBody);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
        }
    }

    /**
     * @param messageBody 消息体
     * @return 消息体
     */
    protected abstract void handleMessage(T messageBody);

}
